package tmi.li.testapp;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class LayoutElementList {
	
	private ArrayList<LayoutElement> _elements = new ArrayList<LayoutElement>();
	public boolean hasSelected = false;
	private int _indexSelected = -1;
	private Paint mPaint;
	
	public LayoutElementList() {
		mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xFFDDDDDD);
        mPaint.setTextSize((float)30);
        
	}

	public LayoutElement getSelectedElement() {
		if(hasSelected && _indexSelected != -1)
			return _elements.get(_indexSelected);
		return null;
	}
	
	public void resetSelected() {
		for(LayoutElement obj : _elements) {
			obj.isSelected = false;
		}
		_indexSelected = -1;
		hasSelected = false;
	}
	
	public void add(LayoutElement obj) {
		_elements.add(obj);
	}
	
	public void remove(int wid, int he) {
		if(!hasSelected)
			return;
		LayoutElement obj = _elements.get(_indexSelected);
		Coordinates coords = obj.getCoords();
		if(wid/2-16 >= coords.getX()-50 && he-30 >= coords.getY()-50 &&
		   wid/2-16 <= coords.getX()+50 && he-30 <= coords.getY()+50) {
			
			_elements.remove(obj);
			resetSelected();
			
		}
			
	}
	
	public boolean match(int x, int y) {
		Coordinates coords;
		for(LayoutElement obj : _elements) {
			coords = obj.getCoords();
			if(x >= coords.getX()-50 && y >= coords.getY()-30 &&
			   x <= coords.getX()+50 && y <= coords.getY()+30) {
				hasSelected = true;
				_indexSelected = _elements.indexOf(obj);
				return true;
			}
		}
		return false;

	}
	
	public boolean matchSelected(int x, int y) {
		if(!hasSelected)
			return false;
		Coordinates coords;
		coords = _elements.get(_indexSelected).getCoords();
		if(x >= coords.getX()-50 && y >= coords.getY()-30 &&
		   x <= coords.getX()+50 && y <= coords.getY()+30) {
			return true;
		}
		return false;
	}
	
	public void moveSelected(int x, int y) {
		_elements.get(_indexSelected).getCoords().setX(x);
		_elements.get(_indexSelected).getCoords().setY(y);
	}
	
	public void draw(Canvas canvas) {
		Coordinates coords;
		for(LayoutElement obj : _elements) {
			coords = obj.getCoords();
			if(hasSelected && obj == _elements.get(_indexSelected)) {
				mPaint.setShadowLayer(3, 1, 1, Color.LTGRAY);
				canvas.drawText("Test", coords.getX(), coords.getY(), mPaint);
			}else {
				mPaint.setShadowLayer((float)0, coords.getX(), coords.getY(), Color.CYAN);
				canvas.drawText("Test", coords.getX(), coords.getY(), mPaint);
			}
			
		}
		
	}
	
}
